package net.wangds.jpa.ctrl;

import net.wangds.data.DisableEnum;
import net.wangds.data.Disableable;
import net.wangds.data.RemovedEnum;
import net.wangds.data.WithId;
import net.wangds.jpa.Sorts;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.convertor.Page2MultipleRespConv;
import net.wangds.jpa.convertor.PagingForm2PageRequestConv;
import net.wangds.log.helper.LogHelper;
import net.wangds.web.entrypoint.BaseMultipleResp;
import net.wangds.web.entrypoint.BaseResp;
import net.wangds.web.entrypoint.PagingForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Optional;

public abstract class AbstractJpaCompCtrl<T extends WithId<String>> {


    protected abstract AbstractRemovedComp<T> fetchJpaComp();

    /**
     * 将查询结果页转换为Http应答.
     */
    @Autowired
    protected Page2MultipleRespConv<T> mmConvPage2Resp;


    /**
     * 将分页查询请求转换为查询分页请求.
     */
    protected PagingForm2PageRequestConv mmConvPf2Pr = new PagingForm2PageRequestConv(() -> Sorts.createTm_desc);

    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public ResponseEntity<BaseResp<T>> ById(
            @PathVariable("id") String id
    ){
        Optional<T> opt = fetchJpaComp().findById(id);
        if(opt.isPresent()) {
            return ResponseEntity.ok(BaseResp.of(opt.get()));
        }else{
            return ResponseEntity.notFound().build();
        }
    }


    @RequestMapping(value="/list", method = RequestMethod.POST)
    public BaseMultipleResp<T> list(
            @RequestBody PagingForm page){
        LogHelper.dev("keywords:"+page.getKeywords());
        if(StringUtils.isBlank(page.getKeywords())) {
            return mmConvPage2Resp.convert(fetchJpaComp().findAll(mmConvPf2Pr.convert(page)));
        } else {
            return listWithKeywords(page);
        }
    }

    protected abstract BaseMultipleResp<T> listWithKeywords(
            @RequestBody PagingForm page);

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public ResponseEntity<BaseResp<T>> put(
            @RequestBody T target
    ){
        if(target.getRemoved()==null){
            target.setRemoved(RemovedEnum.NORMAL);
        }
        if(target instanceof Disableable){
            Disableable t = (Disableable) target;
            if(t.getDisable()==null){
              t.setDisable(DisableEnum.NORMAL);
            }
        }
        T res = fetchJpaComp().save(target);
        if(res!=null) {
            return ResponseEntity.ok(BaseResp.of(res));
        }else{
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(BaseResp.<T>of(null).code(-1).msg("保存失败"));
        }
    }

    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public ResponseEntity<BaseResp<Boolean>> delete(
            @PathVariable("id") String id){
        fetchJpaComp().remove(id);
        return ResponseEntity.ok(BaseResp.of(true));
    }


}
